﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ClientAppraise.ViewModels.Validator
{
    public sealed class ZxRegularExpressionAttribute : ValidationAttribute
    {
        string strReg;

        public ZxRegularExpressionAttribute(string strReg, string errMsg) : base(errMsg)
        {
            this.strReg = strReg;
        }

        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            if (value == default) return ValidationResult.Success;

            var strValue = value.ToString();

            if (string.IsNullOrEmpty(strValue)) return ValidationResult.Success;


            Regex reg = new Regex(strReg);

            if (!reg.IsMatch(strValue))
            {
                return new(ErrorMessage);
            }

            return ValidationResult.Success;
        }
    }
}
